﻿namespace GuiaTest
{
    using System;
    using System.Windows.Controls;
    using Guia.Core;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using WpfTestApplication;
    using CheckBox = Guia.Elements.CheckBox;
    using ComboBoxItem = Guia.Elements.ComboBoxItem;
    using ListBox = Guia.Elements.ListBox;
    using ListBoxItem = Guia.Elements.ListBoxItem;
    using ListView = Guia.Elements.ListView;

    /// <summary>
    /// Summary description for ListBoxTest
    /// </summary>
    [TestClass]
    public class ListViewTest : AbstractWindowTest
    {
        protected override Type TestWindowType()
        {
            return typeof(ListViewTestWindow);
        }

        protected override string TestWindowName()
        {
            return "ListViewTestWindow";
        }

        [TestMethod]
        public void TestGetSubItem()
        {
            ListView listView = window.Get<ListView>("listViewSimple");
            CheckBox listItemCheckBox = listView.GetSubItem<CheckBox>("item3CheckBox");
            listItemCheckBox.Check();
            Assert.IsTrue(listItemCheckBox.IsChecked);
        }

        [TestMethod]
        [ExpectedException(typeof(ElementNotFoundException))]
        public void TestGetSubItemNotExisting()
        {
            ListView listView = window.Get<ListView>("listViewSimple");
            CheckBox listItemCheckBox = listView.GetSubItem<CheckBox>("item44CheckBox");
            Assert.IsFalse(listItemCheckBox.IsChecked);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestGetSubItemNullValue()
        {
            ListView listView = window.Get<ListView>("listViewSimple");
            CheckBox listItemCheckBox = listView.GetSubItem<CheckBox>(null);
        }

        #region ListBoxTest: These tests are clones of ListBoxTest, because the ListView extends the ListBox.

        [TestMethod]
        public void TestGetSelectedItem()
        {
            ListView listView = window.Get<ListView>("listViewSimple");
            ListBoxItem[] selItems = listView.SelectedItems;
            Assert.AreEqual(0, selItems.Length);
            listView.GetItem("Item 1").Select();
            selItems = listView.SelectedItems;
            Assert.AreEqual(1, selItems.Length);
            Assert.AreEqual("Item 1", selItems[0].TextValue);
        }

        [TestMethod]
        public void TestIsSelected()
        {
            ListView listView = window.Get<ListView>("listViewSimple");
            ListBoxItem[] selItems = listView.SelectedItems;
            Assert.AreEqual(0, selItems.Length);
            listView.GetItem("Item 1").Select();
            Assert.IsTrue(listView.GetItem("Item 1").IsSelected);
            listView.GetItem("Item 1").Unselect();
            Assert.IsFalse(listView.GetItem("Item 1").IsSelected);
        }

        [TestMethod]
        public void TestGetItemsEmpty()
        {
            ListView listBox = window.Get<ListView>("listViewEmpty");
            ListBoxItem[] items = listBox.Items;
            Assert.AreEqual(0, items.Length);
        }

        [TestMethod]
        public void TestGetItems()
        {
            ListView listBox = window.Get<ListView>("listViewSimple");
            ListBoxItem[] items = listBox.Items;
            Assert.AreEqual(3, items.Length);
            Assert.AreEqual("Item 1", items[0].TextValue);
            Assert.AreEqual("Item 2", items[1].TextValue);
            // The empty item is the listviewitem with the checkbox
            Assert.AreEqual("", items[2].TextValue);
        }

        [TestMethod]
        public void TestContains()
        {
            ListView listBox = window.Get<ListView>("listViewSimple");
            Assert.IsTrue(listBox.Contains("Item 1"));
            Assert.IsTrue(listBox.Contains("Item 2"));
            Assert.IsTrue(listBox.Contains(""));
            Assert.IsFalse(listBox.Contains("Select me!"));
        }

        [TestMethod]
        public void TestGetItem()
        {
            ListView listBox = window.Get<ListView>("listViewSimple");
            ComboBoxItem item = listBox.GetItem("Item 2");
            Assert.IsNotNull(item);
            Assert.AreEqual("Item 2", item.TextValue);
        }

        [TestMethod, ExpectedException(typeof(ElementNotFoundException))]
        public void TestGetItemNotExisting()
        {
            ListView listBox = window.Get<ListView>("listViewSimple");
            ListBoxItem item = listBox.GetItem("Select me!");
        }

        [TestMethod]
        public void TestGetItemWithIndexer()
        {
            ListView listBox = window.Get<ListView>("listViewSimple");
            ComboBoxItem item = listBox[1];
            Assert.IsNotNull(item);
            Assert.AreEqual("Item 2", item.TextValue);
        }

        [TestMethod, ExpectedException(typeof(ElementNotFoundException))]
        public void TestGetItemNotExistingWithIndexer()
        {
            ListView listBox = window.Get<ListView>("listViewSimple");
            ListBoxItem item = listBox[4];
        }

        #endregion
    }
}
